﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
//using System.Data;
using System.Drawing;
//using System.Linq;
//using System.Text;
using System.Windows.Forms;
//using CANoe;

namespace TestCANoE
{
    public partial class Form1 : Form
    {
        private string canoeConfFile;
        private static CANoe.IApplication canoeApp;
        private static CANoe.IMeasurement canoeMeasure;

        private string step;

        public Form1()
        {
            InitializeComponent();
            //add private CANoE var init code;
            try
            {
                canoeApp = new CANoe.Application();
            }
            catch (Exception err) {
                MessageBox.Show(err.Message);
                //this.Dispose();
            }

            //this.canoeMeasure = canoeApp.Measurement();
        }

        private void browseFileButtion_Click(object sender, EventArgs e)
        {
            DialogResult canoeConfResult = this.canoeConfFileDialog.ShowDialog();
            //first get the config file path.
            if (canoeConfResult == DialogResult.OK)
            {
                this.canoeConfFile = this.canoeConfFileDialog.FileName;
                this.textBox1.Text = this.canoeConfFile;
            }
            else {
                return;
            }
            //2nd start CANoE and start to run the simulation.
        }

        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                canoeApp.Open(this.canoeConfFile);
                step = "OPEN APP DONE.";
                canoeMeasure = canoeApp.Measurement;
                step = "init Measurement done.";

                

                canoeMeasure.Start();
                //System.Threading.Thread.Sleep(2000);
                //this.canoeApp.Measurement().Start();
                step = "measurement start done.";
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + step);
                return;
            }

            MessageBox.Show("CANoe start Done.");
            return;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //if(canoeMeasure.)
            //if (canoeMeasure.Application.Running) {
                canoeMeasure.Stop();
                MessageBox.Show("measurement stopped.");
                canoeApp.Quit();
                MessageBox.Show("app quit.");
            //}

        }


    }
}
